/**
 * 应用变更js
 */

import {Util,I18Parse,CommonLang} from "@lowcode/jpaas-common-lib"
import ChangeAppDialog from './ChangeAppDialog'
import bpmDefLang from '../../../locales/lang/impl/bpm/bpmDefLang'


let lang = bpmDefLang.bpmDef;
let i18n=function (langKey,text) {
    return I18Parse.getI18Val(lang,langKey, text)
}

export default {
    data() {
        return {
            //是否为应用开发模式
            isAppDevelop:false
        }
    },
    created() {
        this.isAppDevelop=this.curApp && this.curApp.appId ? true:false
    },
    methods:{
        /**
         * @param catKey 分类key FORM、BPM、SYSTEM
         * @param selectedRows 需要修改的数据
         * @param pkId 主键字段
         * @param name 名称字段
         * @param appId 应用Id字段
         * @param categoryId 分类Id字段
         * @param saveClick 回调保存方法
         */
        changeApp(catKey,selectedRows,pkId,name,appId,categoryId,saveClick){
            if(!selectedRows || selectedRows.length==0){
                this.$message.warning(i18n('changeAppTips','至少选择一条数据!'));
                return;
            }
            var data=[];
                selectedRows.find(row=>{
                    data.push({
                        pkId:row[pkId],
                        name:row[name],
                        appId:row[appId],
                        newAppId:"",
                        categoryId:row[categoryId],
                        newCategoryId:""
                    })
                })


            var conf = {
                curVm: this,
                data: {data: data,catKey:catKey},
                max:true,
                //widthHeight: ['1000px', '600px'],
                component: ChangeAppDialog,
                title: i18n('list.changeApp','变更应用')
            };
            var self=this;
            Util.open(conf,function(action,data){
                if(action!='ok')return;
                var newRows= self.mergeData(selectedRows,data,pkId,appId,categoryId);
                if(saveClick){
                    saveClick(newRows);
                }
            })
        },
        //合并数据
        mergeData(oldData,newData,pkId,appId,categoryId){
            for (let i = 0; i < oldData.length; i++) {
                for (let j = 0; j < newData.length; j++) {
                    if(oldData[i][pkId]==newData[j].pkId){
                        oldData[i][appId]=newData[j].newAppId;
                        oldData[i][categoryId]=newData[j].newCategoryId;
                        break;
                    }
                }
            }
            return oldData;
        }
    }
}